﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace YXShop.Web.membercenter.product
{
    public partial class groupbuyinfo_list : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (ChangeHope.WebPage.PageRequest.GetFormString("Option") != string.Empty && ChangeHope.WebPage.PageRequest.GetFormInt("id") > 0)
                {
                    string types = Request.Form["Option"].Trim();
                    int id = ChangeHope.WebPage.PageRequest.GetFormInt("id");
                    if (types == "del")
                    {
                        del(id);
                    }
                    Response.End();
                    return;
                }
                
                GetList();
            }
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        protected void GetList()
        {
            string cond = string.Empty;
            if (Session["MemberID"] != null)
            {
                cond += " and uid=" + Session["MemberID"].ToString() + "";
                this.hlGroupBuyInfo.NavigateUrl = "onlinesignupinfo_list.aspx?w_d_uid=" + Session["MemberID"].ToString();
            }
            YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();
            ChangeHope.WebPage.Table table = new ChangeHope.WebPage.Table();
            YXShop.BLL.Product.Product_SendGroupBuyInfo data = new YXShop.BLL.Product.Product_SendGroupBuyInfo();
            ChangeHope.DataBase.DataByPage dataPage = data.GetList(" [order by] id asc", 20, cond);

            YXShop.BLL.Product.GroupBuy_OnlineSignUpInfo pobll = new YXShop.BLL.Product.GroupBuy_OnlineSignUpInfo();

            YXShop.BLL.Product.Product_IntegratePurchas pibll = new YXShop.BLL.Product.Product_IntegratePurchas();
            YXShop.Model.Product.Product_IntegratePurchas pimodel = null;
            DataTable dt = null;
            //第一步先添加表头
            table.AddHeadCol("10%", "活动编号");
            table.AddHeadCol("10%", "活动名称");
            table.AddHeadCol("15%", "发布时间");
            table.AddHeadCol("15%", "报名情况");
            table.AddHeadCol("12%", "活动状态");
            table.AddHeadCol("8%", "审核");
            table.AddHeadCol("20%", "操作");
            table.AddRow();
            //添加表的内容
            if (dataPage.DataReader != null)
            {
                while (dataPage.DataReader.Read())
                {
                    //查看报名人数
                    int count = 0;
                    dt = pobll.GetActivityList(dataPage.DataReader["id"].ToString());
                    if (dt.Rows.Count > 0)
                    {
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            count += Convert.ToInt32(dt.Rows[i]["signupcount"].ToString());
                        }
                    }
                    //查看价格等级
                    pimodel = pibll.GetModelProductID(Convert.ToInt32(dataPage.DataReader["productid"].ToString()));
                    string strTitle = "请查看你组团所享受的价格：\n";
                    if (pimodel != null)
                    {
                        string[] LadderCount = pimodel.LadderCount.Split(',');
                        string[] LadderPrice = pimodel.LadderPrice.Split(',');
                        for (int f = 0; f < LadderCount.Length; f++)
                        {
                            strTitle += "报名人数达到" + LadderCount[f] + ",可享受价格为" + LadderPrice [f]+ "元\n";
                        }
                    }
                    table.AddCol("No." + dataPage.DataReader["id"].ToString());
                    table.AddCol(dataPage.DataReader["title"].ToString());
                    table.AddCol(dataPage.DataReader["createtime"].ToString());

                    table.AddCol(string.Format("" + count.ToString() + "人已报名（<a href='onlinesignupinfo_list.aspx?w_d_sid={0}' title='" + strTitle + "'>查看</a>）", dataPage.DataReader["id"].ToString()));
                    table.AddCol(ActivityState(dataPage.DataReader["activitystarttime"].ToString(), dataPage.DataReader["activityendtime"].ToString()));
                    table.AddCol(dataPage.DataReader["state"].ToString()=="0"?"未通过":"通过");
                    table.AddCol(string.Format("<a href='sendgroupbuyinfo.aspx?id={0}'>详情</a>", dataPage.DataReader["id"].ToString()));
                    table.AddRow();
                }
            }
            string view = table.GetTable() + dataPage.PageToolBar;
            dataPage.Dispose();
            dataPage = null;
            this.litInfo.Text = view;
        }
       
        protected string ActivityState(string starttime,string endtime)
        {
            string reStr = "";
            if (Convert.ToDateTime(starttime) <= System.DateTime.Now && System.DateTime.Now < Convert.ToDateTime(endtime))
            {
                reStr = "正在召集中...";
            }
            else if (System.DateTime.Now > Convert.ToDateTime(endtime))
            {
                reStr = "报名以结束";
            }
            else if (System.DateTime.Now < Convert.ToDateTime(starttime))
            {
                reStr = "报名还未进行";
            }
            return reStr;
        }

        private void del(int id)
        {
            YXShop.BLL.Product.Product_SendGroupBuyInfo bll = new YXShop.BLL.Product.Product_SendGroupBuyInfo();
            bll.Delete(id);
            Response.Write("ok");
        }
    }
}

